package cn.iocoder.yudao.framework.datasource.processor;

import com.baomidou.dynamic.datasource.processor.DsProcessor;
import org.aopalliance.intercept.MethodInvocation;
import org.springframework.aop.framework.ReflectiveMethodInvocation;

/**
 * TODO
 *
 * @className: DsPackageProcessor
 * @author: tangkh
 * @date: 2023-09-19 17:57
 */
public class DsPackageProcessor extends DsProcessor {
    private static final String PREFIX = "cloud_health_";

    public DsPackageProcessor() {
    }

    @Override
    public boolean matches(String key) {
        return true;
    }

    @Override
    public String doDetermineDatasource(MethodInvocation invocation, String key) {
        Class aClass = ((ReflectiveMethodInvocation)invocation).getProxy().getClass();
        String aPackage = aClass.toString().replaceAll("class ", "");
        String[] aPackageSplit = aPackage.split("\\.");
        if (!aPackage.contains("flow.oa") && !aPackage.contains("flow.track")) {
            if (aPackageSplit != null && aPackageSplit.length >= 3) {
                return "cloud_health_" + aPackageSplit[2];
            }
        } else if (aPackageSplit != null && aPackageSplit.length >= 4) {
            return "cloud_health_" + aPackageSplit[2] + "_" + aPackageSplit[3];
        }

        return "";
    }
}